<?php
class SaveGroup extends Action {
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$group = rqsts ( array ('gid' => 0, 'gname', 'name','type','note' ), true );
		$groupModel = new CoreGroupModel ();
		if (empty ( $group ['gid'] )) { // 新增
			unset ( $group ['gid'] );
		}
		if (! rqset ( 'gname' )) {
			unset ( $group ['gname'] );
		}
		//验证用户数据的合法性
		$valid = $this->validate ( 'group', $group, $groupModel );
		if ($valid === true) { //用户数据合法			
			$rst = $groupModel->save ( $group );
			if (! empty ( $rst )) {
				if (! rqset ( 'goon' )) {
					sess_add ( 'page_tip_info', '<strong>恭喜,</strong>用户组修改成功.' );
					Response::redirect ( './?Ctlr=EditGroup&suc=1&gid=' . $rst ['gid'] );
				} else {
					Response::redirect ( './?Ctlr=AddGroup' );
				}
			} else {
				sess_add ( 'group_info', $group );
				sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $groupModel->last_error_msg () );
				Response::redirect ( $this->referer );
			}
		}
		sess_add ( 'group_error', $valid );
		sess_add ( 'group_info', $group );
		Response::redirect ( $this->referer );
	}
}